package com.lagou.edu.controller;

import com.lagou.edu.api.ResumeServiceApi;
import com.lagou.edu.common.BaseResult;
import com.lagou.edu.common.ResultEum;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author renpeiqian
 * @date 2021/7/13 16:12
 * @since
 */
@RestController
@RequestMapping("/autodeliver")
@Slf4j
public class AutoDeliverController {

     @Autowired
     RestTemplate restTemplate;

     //注册服务发现客户端
     @Autowired
    DiscoveryClient discoveryClient;

     @Autowired
    ResumeServiceApi resumeServiceApi;

     /**
      * @Description 根据用户id调用简历服务查询简历开放状态
      * @Param [userId]
      * @return java.lang.Integer
      */
     @RequestMapping("/checkState/{userId}")
     public Integer findResumeOpenState(@PathVariable String userId){
         Integer openState = restTemplate.getForObject("http://localhost:8080/resume/openstate/" + userId, Integer.class);
         log.info("调用简历微服务查询简历状态接口，获取到用户：【"+userId+"】的默认简历开放状态为:【"+openState+"】");
        return openState;
     }

     /**
      * @Description
      * 从注册中心获取对应服务的的注册实例，根据实例获取ip和端口，发送http请求。
      * @Param [userId]
      * @return java.lang.Integer
      */
    @RequestMapping("/checkStateByEureka/{userId}")
    public Integer findResumeOpenStateByEureka(@PathVariable String userId){

        //1.根据服务名从注册中心获取服务实例
        List<ServiceInstance> instances = discoveryClient.getInstances("lagou-service-resume");
        //2.不考虑负载均衡，获取第1个实例
        ServiceInstance instance = instances.get(0);
        //3.拼接url
        String host = instance.getHost();
        int port = instance.getPort();
        String url ="http://" + host + ":" + port + "/resume/openstate/" + userId;

        //4.获取实例端口
        log.info("实例host:{},实例端口：{},url:{}",host,port,url);
        Integer openState = restTemplate.getForObject(url ,Integer.class);
        log.info("调用简历微服务查询简历状态接口，获取到用户：【"+userId+"】的默认简历开放状态为:【"+openState+"】");
        return openState;
    }


    /**
     * 提供者模拟处理超时，调用方添加hystrix
     * @Param []
     * @return java.lang.String
     */
    @GetMapping("/checkStateTimeOut/{userId}")
    //使用hystrixCommand注解进行熔断控制细节配置
    @HystrixCommand(
            threadPoolKey = "checkStateTimeOut",  //线程池标识，保持唯一。相同key标识公用
            //线程池配置属性
            threadPoolProperties = {
                   @HystrixProperty(name="coreSize",value = "1"), //线程数
                   @HystrixProperty(name="maxQueueSize",value = "20")   //等待队列长度

             },
            //熔断细节配置
            commandProperties = {
                    @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000")  //请求超时时间
            }
    )
    public Integer checkStateTimeOut(@PathVariable Long userId){
        String url ="http://lagou-service-resume/resume/state/"+userId ;
        Integer state = restTemplate.getForObject(url, Integer.class);

        return  state;
    }

    /**
     * @Description ：根据用户id查看用户简历开发状态
     * @Param [userId] 用户id
     * @return com.lagou.edu.common.BaseResult<java.lang.Integer>
     */
    @RequestMapping(value = "/checkStateByFeign/{userId}",method = RequestMethod.POST)
    public BaseResult<Integer> checkStateByFeign(@PathVariable Long userId){
        BaseResult<Integer> result = new BaseResult<>();
        result.setCode(ResultEum.SUCCESS.getCode());
        result.setMessage(ResultEum.SUCCESS.getMessage());
        Integer check = resumeServiceApi.check(userId);
        result.setData(check);
        return result;
    }






}
